package com.baojianren.bean;

import java.awt.Color;
import java.awt.Graphics;

import com.baojianren.tank.Dir;
import com.baojianren.tank.TankFrame;

/**
 * @author ௸྄ིོུ倾心คิดถ
 * @version 创建时间：2020年12月26日 下午9:00:52
 * @ClassName Tank
 * @Description 坦克实体类
 */
public class Tank {
	private int x, y; // 坐标
	private Dir dir = Dir.DOWN;// 初始出现方向键盘下
	private static final int SPEED = 5;// 坦克移动速度
	private boolean moving = false;// 控制坦克移动还是禁止
	private TankFrame frame = null;

	public Tank() {
	}

	public Tank(int x, int y, Dir dir, TankFrame tf) {
		super();
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.frame = tf;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Dir getDir() {
		return dir;
	}

	public void setDir(Dir dir) {
		this.dir = dir;
	}

	public boolean isMoving() {
		return moving;
	}

	public void setMoving(boolean moving) {
		this.moving = moving;
	}

	/**
	 * 绘制坦克
	 * 
	 * @param g
	 */
	public void paint(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.YELLOW);
		g.fillRect(x, y, 50, 50);
		g.setColor(c);
		move();
	}

	/**
	 * 控制坦克移动方向和停止移动
	 */
	private void move() {
		if (!moving)
			return;
		switch (dir) {
		case LEFT:
			x -= SPEED;
			break;
		case UP:
			y -= SPEED;
			break;
		case RIGHT:
			x += SPEED;
			break;
		case DOWN:
			y += SPEED;
			break;
		case A:
			x -= SPEED;
			break;
		case W:
			y -= SPEED;
			break;
		case D:
			x += SPEED;
			break;
		case S:
			y += SPEED;
			break;

		default:
			break;
		}
	}

	/**
	 * 发射子弹
	 */
	public void fire() {
		frame.bullets.add( new Bullet(this.x, this.y, this.dir,this.frame));
	}

}
